Imports Microsoft.VisualBasic
Imports System.Collections.Generic

Namespace MyDAO
    Public Class SelectStringBuilder
        Inherits SqlStringBuilder

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal tableName As String)
            MyBase.New(tableName)
        End Sub

        Public Sub New(ByVal tableName As String, ByVal aliasName As String)
            MyBase.New(tableName, aliasName)
        End Sub

        Public Sub New(ByVal view As SelectStringBuilder, ByVal aliasName As String)
            Me.Table = New MySubqueryTable(view, aliasName)
        End Sub

        Public Sub New(ByVal query As MySubqueryTable)
            Me.Table = query
            'Me.Table.AliasName = aliasName
        End Sub

        Public Columns As New List(Of MyDataColumn)

        Public Sub AddColumn(ByVal ParamArray columnNames() As String)
            For Each columnName As String In columnNames
                Me.AddColumn(New MyDataColumn(columnName))
            Next
        End Sub

        Public Sub AddColumn(ByVal column As MyDataColumn)
            Me.Columns.Add(column)
        End Sub

        Public SortColumns As New List(Of MyDataSortColumn)

        Public Sub AddSortColumn(ByVal columnName As String)
            Me.AddSortColumn(columnName, MyDataSortDirection.Ascending)
        End Sub

        Public Sub AddSortColumn(ByVal columnName As String, ByVal diretion As MyDataSortDirection)
            Me.SortColumns.Add(New MyDataSortColumn(columnName, diretion))
        End Sub

        Public GroupColumns As New List(Of MyDataExpressionBase)

        Public Sub AddGroupColumn(ByVal ParamArray columnNames() As String)
            For Each columnName As String In columnNames
                Me.GroupColumns.Add(New MyColumnExpression(columnName))
            Next
        End Sub

        Public Distinct As Boolean

        Public Unions As New List(Of MyDataUnion)

        Public Sub AddUnion(ByVal builder As SelectStringBuilder)
            Me.Unions.Add(New MyDataUnion(builder))
        End Sub

        Public Function Union() As MyDataUnion
            Return New MyDataUnion(Me)
        End Function

        Public Function UnionAll() As MyDataUnion
            Return New MyDataUnion(Me, True)
        End Function

        Public Overrides Function Build() As MyStringBuilder
            Dim sql As New MyStringBuilder

            sql.AppendFormat("select ")

            If Me.Distinct = True Then
                sql.AppendFormat("distinct ")
            End If

            Dim expressions As New List(Of String)

            'column
            expressions.Clear()

            For Each dataColumn As MyDataColumn In Me.Columns
                expressions.Add(dataColumn.ToString())
            Next

            sql.AppendFormat(String.Join(",", expressions.ToArray()))
            sql.AppendFormat(" ")

            'table
            sql.AppendFormat("from {0} ", Me.Table)

            'join
            expressions.Clear()

            For Each relation As MyDataRelationBase In Me.Relations
                'sql.AppendFormat("
                expressions.Add(relation.ToString())
            Next

            sql.AppendFormat(String.Join(" ", expressions.ToArray()))
            sql.AppendFormat(" ")

            'condition
            If Me.Condition IsNot Nothing Then
                sql.AppendFormat("where ")
                sql.AppendFormat(Me.Condition.ToString())
                sql.AppendFormat(" ")
            End If

            'group column
            If Me.GroupColumns.Count > 0 Then
                sql.AppendFormat("group by ")

                expressions.Clear()

                For Each groupColumn As MyDataExpressionBase In Me.GroupColumns
                    expressions.Add(groupColumn.ToString())
                Next

                sql.AppendFormat(String.Join(",", expressions.ToArray()))
                sql.AppendFormat(" ")
            End If

            'union
            expressions.Clear()

            For Each union As MyDataUnion In Me.Unions
                expressions.Add(union.ToString())
            Next

            sql.AppendFormat(String.Join(" ", expressions.ToArray()))
            sql.AppendFormat(" ")

            'sort column
            If Me.SortColumns.Count > 0 Then
                sql.AppendFormat("order by ")

                expressions.Clear()

                For Each sortColumn As MyDataSortColumn In Me.SortColumns
                    expressions.Add(sortColumn.ToString())
                Next

                sql.AppendFormat(String.Join(",", expressions.ToArray()))
                sql.AppendFormat(" ")
            End If

            'For Each dataCondition As MyDataConditionBase In Me.Conditions
            'Dim exp As New StringBuilder

            'exp.AppendFormat(dataCondition.ToString())
            'exp.AppendFormat(dataCondition.op.ToString())

            'If dataCondition.op Is MyDataOperator.Equal Or dataCondition.op Is MyDataOperator.NotEqual Then
            '    exp.AppendFormat(dataCondition.Expression2.ToString())
            'Else
            '    exp.AppendFormat("(" + dataCondition.Expression2.ToString() + ")")
            'End If

            'expressions.Add(dataCondition.ToString())
            'Next

            'sql.AppendFormat(String.Join(" and ", expressions.ToArray()))

            Return sql
        End Function

    End Class
End Namespace
